package com.yc.mvc.web;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 自定义转换器
 */
@Component
public class TimeConvertor implements Converter<String, Timestamp> {
    @Override
    public Timestamp convert(String source) {
        System.out.println("source = " + source);
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddTHH:mm");
            Date date = sdf.parse(source);
            System.out.println("date = " + date);
            // date 转 timestamp
            return new Timestamp(date.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
